//*************************************************************************
//
//      Code by Michael Carey
//      for Dr. Ostermann's CS444 Class
//      Ohio University - Spring 2007  
//      
//      Email:          mc12800@ohio.edu
//      Program:        Adena Webserver
//      Filename:       wserver.h
//
//*************************************************************************
#ifndef _W_SERVER_H
#define _W_SERVER_H

#include <string>		/* STL string */
#include <strings.h>		/* bzero function */
#include <netdb.h>		/* getprotobyname function */
#include <errno.h>		/* Error numbers and names */
#include <sys/socket.h>		/* Compiles without */
//#include <netinet/in.h>	/* Compiles without */

using std::string;

class Wserver{
	public:
		Wserver();	/* Default Constructor */

		// Open a socket, default portnum 0 will give random port
		void openSocket(int portnum = 0);	

		int start(); /* Start serving web pages */
		void setPort(int a_port){ s_port = a_port; }
		void setUchk(bool flag){ usr_check = flag; }
		void servDetails(); /* Outputs hostname and port */
		void setDebug(bool flag){ debug = flag; }
	private:
		int request(int skt);	/* Handle 1 request */
		string parseRequest(char *input, size_t size);
		int checkFile(string filename);	/* Check a files permissions */
		string responseHeader(int code, string path, int size); /* Format header */
		bool findRedir(string r_file);	/* Check to see if a .redir file exists */
		void doRedir(string i_file);	/* Perform redirection */
		int getWebSocket(in_addr *address);	/* grab the site we should be rediring */
		int getWebpage(string request, int fd, char *buffer, string filename);	
		void rate_send(int fd, string data, string filename);
		void rate_send(int fd, char *data, string filename, int isize);

		bool debug;	/* Show debug output, default no */
		bool usr_check; /* Should we check to see if we own the files */
		int s_port;	/* If a static port is set, store here */
		int m_socket;	/* Master Socket */
		int s_socket;	/* Slave Sockets */
};

#endif
